package com.scy.java.thread.eatbuns;
//生产包子
public class BaoZiShop extends Thread {
    //包子铺生产包子
    private BaoZi baoZi;

    public BaoZiShop(BaoZi baoZi) {
        this.baoZi = baoZi;
    }

    //设置线程任务生产包子
    @Override
    public void run() {
        //定义一个临时变量
        int count = 0;
        while (true){
            //使用同步代码快保证两个线程只有一个执行
            synchronized (baoZi){
                //保证包子的状态
                if (baoZi.flag){
                    //有包子，等待顾客吃完当前包子
                    try {
                        //调用wait()进入等待状态
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                //没有就开始生产包子
                if(count++ % 2 ==0){
                    //生产剥皮豆腐陷包子
                    baoZi.skin = "冰皮";
                    baoZi.stuffing = "豆腐馅";
                }else {
                    baoZi.skin = "薄皮";
                    baoZi.stuffing = " 牛肉大葱馅";
                }
                System.out.println(
                        "包子铺正在生产:"+baoZi.skin+baoZi.stuffing+"包子");
                try {
                    //生产需要三秒中
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //baoZi.flag = true表示有包子
                baoZi.flag = true;
                System.out.println(
                        "包子铺已经生产好了:"+baoZi.skin
                                +baoZi.stuffing+"包子。可以开始吃了。。。");
                //唤醒消费者程序
                baoZi.notify();
            }
        }
    }
}
